package com.niuniu;

import com.xingli.zhajinhua.beans.Card;
import com.xingli.zhajinhua.beans.PlayerCards;
import com.xingli.zhajinhua.beans.niuniu.enums.NiuCardColor;
import com.xingli.zhajinhua.beans.niuniu.enums.NiuCardPoint;
import com.xingli.zhajinhua.beans.niuniu.enums.NiuCardType;
import com.zhu.spider.ExcelExport;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NiuNiuExport {

    public static void main(String[] args) throws IOException {
        excelExport();
//        csvExport();

    }

    private static int[] winPlayRecord = new int[]{0,0,0};
    private static Map<Integer,Integer> continueWinMap = new HashMap<>();
    private static void excelExport() throws IOException {
        int length = 65004;
        ExcelExport excelExport = new ExcelExport("E:\\牛牛"+length+"条.xlsx");
        String[] head = new String[]{"玩家1牌","类型","对决","玩家2牌","类型","赢家"};
        excelExport.setRowContent(head);
        for (int i = 0; i < length; i++) {
            NiuNiuLogic niuNiuGame = new NiuNiuLogic();
            List<Card> cardList = niuNiuGame.getNewCardCollectionAndShuffle();
            List<Card[]> cardsList = niuNiuGame.sendCards(cardList);
            List<PlayerCards> niuCardsDataList = niuNiuGame.compareCards(cardsList);
            String[] str = new String[6];
            str[0] = getCardStr(niuCardsDataList.get(0).getCards());
            str[1] = getCardTypeStr(niuCardsDataList.get(0).getCardsType());

            str[2] = "VS";

            str[3] = getCardStr(niuCardsDataList.get(1).getCards());
            str[4] = getCardTypeStr(niuCardsDataList.get(1).getCardsType());

            int winIndex = 0;
            if (niuCardsDataList.get(0).getCardsType().equals(niuCardsDataList.get(1).getCardsType())){
                str[5] = "平局";
                winIndex = 2;
            }else {
                str[5] = niuCardsDataList.get(0).isResult()?"玩家1":"玩家2";
                winIndex = niuCardsDataList.get(0).isResult()?0:1;
            }
            calContinueWinMap(winIndex);
            excelExport.setRowContent(str);
            System.out.println(i);
        }

        excelExport.setRowContent(continueWinMap.toString());
        excelExport.exportContent();
    }
    private static void calContinueWinMap(int winIndex){
        int temp = winPlayRecord[winIndex];
        for (int i = 0; i < 3; i++) {

            if (winIndex != i){
                continueWinMap.putIfAbsent(winPlayRecord[i],0);
                continueWinMap.put(winPlayRecord[i],continueWinMap.get(winPlayRecord[i])+1);
            }

            winPlayRecord[i] = 0;
        }
        winPlayRecord[winIndex] = temp+1;
    }
    private static void csvExport() throws IOException {
        int length = 20000;
        ExcelExport excelExport = new ExcelExport("E:\\牛牛"+length+"条.xlsx");
        String[] head = new String[]{"玩家1牌","类型","对决","玩家2牌","类型","赢家"};
        excelExport.setRowContent(head);

        for (int i = 0; i < length; i++) {
            NiuNiuLogic niuNiuGame = new NiuNiuLogic();
            List<Card> cardList = niuNiuGame.getNewCardCollectionAndShuffle();
            List<Card[]> cardsList = niuNiuGame.sendCards(cardList);
            List<PlayerCards> niuCardsDataList = niuNiuGame.compareCards(cardsList);
            String[] str = new String[6];
            str[0] = getCardStr(niuCardsDataList.get(0).getCards());
            str[1] = getCardTypeStr(niuCardsDataList.get(0).getCardsType());

            str[2] = "VS";

            str[3] = getCardStr(niuCardsDataList.get(1).getCards());
            str[4] = getCardTypeStr(niuCardsDataList.get(1).getCardsType());

            if (niuCardsDataList.get(0).getCardsType().equals(niuCardsDataList.get(1).getCardsType())){
                str[5] = "平局";
            }else {
                str[5] = niuCardsDataList.get(0).isResult()?"玩家1":"玩家2";
            }

            excelExport.setRowContent(str);
            excelExport.exportContent();
            System.out.println(i);
        }

    }
    private static String getCardStr(Card[] cards){
        StringBuilder str = new StringBuilder();
        for (Card card:cards){
            str.append(NiuCardColor.values()[card.getColor()].name+ NiuCardPoint.values()[card.getPoint()].name+" ");
        }
        return str.toString();
    }
    private static String getCardTypeStr(String cardType){
        return NiuCardType.valueOf(cardType).name;
    }
}
